package bstsetcode.homework;




/**
 * @author noob
 * @version 1.0
 * @date 2021/3/15 15:00
 * 没有void的删除方法: 需要关联前一个结点和后续的结点连接，要不然没有返回结点，会断
 */
public class Solution5 {

    public ListNode removeElements(ListNode head, int val) {
        ListNode dummyHead = new ListNode(-1,head);
        removeElement(dummyHead,val);
        return dummyHead.next;

    }


    private void removeElement(ListNode head, int val) {
        if(head == null || head.next == null){
            return;
        }

        if (head.next.val == val){
            head.next = head.next.next;
            removeElement(head,val);
        }else{
            removeElement(head.next,val);
        }

    }


    public static void main(String[] args) {
        int[] nums = {1,2,6,3,4,5,6};
        ListNode head = new ListNode(nums);
        System.out.println(head);

        (new Solution5()).removeElements(head,6);
        System.out.println(head);
    }
}
